<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

/**
 * 微信用户管理
 * @package App\Http\Controllers\Admin
 */
class WxUserController extends BaseController
{
    private $userSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->userSer = new \App\Services\WxUserService();
    }

    /**
     * 用户列表
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            //关注渠道
            $query = \App\Models\WxUser::select('*');
            $subscribe_scene = $request->input('subscribe_scene', '');
            if ($subscribe_scene) {
                $query->where('subscribe_scene', $subscribe_scene);
            }
            //用户openid
            $openid = $request->get('openid', '');
            if ($openid) {
                $query->where('openid', $openid);
            }
            //关键字
            $keyword = $request->get('keyword', '');
            if ($keyword) {
                $query->where('nickname', 'like', '%' . $keyword . '%');
            }
            //性别
            $sex = $request->get('sex', -1);
            if ($sex > -1) {
                $query->where('sex', $sex);
            }
            //DB::table('test')->paginate();
            $rows = $query->withTrashed()->paginate($pageSize);
            if ($rows) {
                foreach ($rows as $k => $v) {
                    $v['subscribe_scene_name'] = $v->subscribe_scene_name;
                    $v['sex_name'] = $v->sex_name;
                    $v['tagid_list'] = (array) $v->tagid_list;
                    $v['tagname_list'] = $v->tagname_list;
                    $rows[$k] = $v;
                }
            }
            //$queries = DB::getQueryLog();// 获取查询日志
            //print_r($queries);
            return $rows;
        }
        //角色列表数据
        $tagSer = new \App\Services\WxUserTagService();
        $tagList = $tagSer->getCacheTagList();
        $this->viewData['tagList'] = $tagList;

        //用户关注渠道
        $subscribeSceneList = $this->userSer->getSubscribeSceneList();
        $this->viewData['subscribeSceneList'] = $subscribeSceneList;
        return view('admin.wxuser.index', $this->viewData);
    }

    /**
     * 同步用户
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function sync()
    {
        try {
            \App\Jobs\WxUserDownload::dispatch()->onQueue('low');
            return $this->showMsg('success', '已成功开启用户拉取任务', [], 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 设置用户备注
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function setRemark(Request $request)
    {
        try {
            $this->userSer->setRemark($request->openid, $request->remark);
            return $this->showMsg('success', '设置备注成功', [], 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 设置用户加入黑名单或取消黑名单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function setBlack(Request $request)
    {
        try {
            if ($request->is_lock == 1) {
                $this->userSer->setBlack($request->openids);
            } else {
                $this->userSer->setUnBlack($request->openids);
            }
            return $this->showMsg('success', $request->is_lock ? '拉黑用户成功' : '恢复用户', [], 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 同步黑名单
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function syncBlack()
    {
        try {
            \App\Jobs\WxUserBlackDownload::dispatch()->onQueue('low');
            return $this->showMsg('success', '已成功开启用户拉取任务', [], 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
    /**
     * 批量打标签
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function addTag(Request $request)
    {
        try {
            if (empty($request->openids)) {
                throw new \Exception('请选择打标签的用户');
            }
            $tagIds = !empty($request->tagIds) ? (array)$request->tagIds : [];
            if (strpos($request->openids, ',') !== false) {
                $openids = explode(',', $request->openids);
            } else {
                $openids = [$request->openids];
            }
            $this->userSer->addTag($openids, $tagIds);
            return $this->showMsg('success', '打标签成功', [], 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 设置用户标签
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function setTag(Request $request)
    {
        try {
            if (empty($request->openids)) {
                throw new \Exception('请选择打标签的用户');
            }
            $tagIds = !empty($request->tagIds) ? (array)$request->tagIds : [];
            if (strpos($request->openids, ',') !== false) {
                $openids = explode(',', $request->openids);
            } else {
                $openids = [$request->openids];
            }
            $this->userSer->setTag($openids, $tagIds);
            return $this->showMsg('success', '设置标签成功', [], 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
}
